<template>
	<view class="content">
		<view class="dataBox" v-if="!needChangePwd">
			<label>用户名:</label>
			<input v-model="newname" placeholder="4到16位字母，数字" />
		</view>
		<view class="dataBox">
			<label>密码:</label>
			<input v-model="pwd" placeholder="6-16位数字字母组合" />
		</view>
		<view class="dataBox" v-if="needChangePwd">
			<label>新密码:</label>
			<input v-model="newPwd" placeholder="6-16位数字字母组合" />
		</view>
		<view class="btns">
			<button type="primary" @click="doChange">修改</button>
			<button @click="back">返回</button>
		</view>
	</view>
</template>

<script>
	import canYouCommit from '../../tools/canYouCommit/index.js';
	import doErro from '../../tools/doErro/index.js';
	import store from '@/store/index.js'; //需要引入store
	export default {
		name: "user",
		props: ['needChangePwd'],
		data() {
			return {
				oldname: this.$store.state.uname,
				newname: this.$store.state.uname,
				pwd: "",
				newPwd: ""
			}
		},
		methods: {
			back() {
				this.$emit('back', 'my')
			},
			doChange() {
				let that = this;
				let parms = {};
				if (that.$props.needChangePwd) {
					parms = {
						pwd: that.pwd,
						newPwd: that.newPwd
					}
				} else {
					parms = {
						uname: that.$store.state.uname,
						pwd: that.pwd
					}
				}
				if (canYouCommit.doCommit(parms)) {
					uni.showLoading({
						title: "修改中...",
						mask: true
					});
					uniCloud.callFunction({
						name: "rc_user",
						data: {
							type: "doChange",
							uid: getApp().globalData.uid,
							oldName: that.oldname,
							newname: that.newname,
							pwd: that.pwd,
							newPwd: that.newPwd || 0
						},
						success(res) {
							if (res.result.success == "fail") {
								if (that.oldname == that.newname) {
									uni.showModal({
										content: "新用户名不能和原用户名相同"
									})
								} else {
									doErro.showErro(res.result.code);
								}
							} else {
								uni.showModal({
									content: "修改成功"
								});
								store.commit('douname', {
									uname: that.newname
								});
							}
							uni.hideLoading();
						},
						fail(err) {
							doErro.showErro(500);
						}
					})
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		font-size: 38rpx;

		box-sizing: border-box;
		padding-left: 10rpx;
		padding-right: 10rpx;

		.dataBox {
			height: 100rpx;
			line-height: 100rpx;
			width: 100%;
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			border-bottom: 1rpx solid #00aa7f;

			label {
				width: 20%;
			}

			input {
				width: 80%;
				height: 100rpx;
				line-height: 100rpx;
			}
		}

		.btns {
			width: 100%;
			display: flex;
			flex-direction: column;
			margin-top: 40rpx;
			border: 0rpx solid red;

			button {
				height: 100rpx;
				line-height: 100rpx;
			}
		}
	}
</style>